package org.example.cloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.cloud.entities.Storage;
import org.example.cloud.mapper.StorageMapper;
import org.example.cloud.service.StorageService;
import org.springframework.stereotype.Service;

/**
 * @author 宋孟林
 * @description
 * @date: 2025/4/12 9:28
 */
@Service
@Slf4j
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage> implements StorageService {
    @Override
    public void decrease(Long productId, Integer count) {
        log.info("开始扣减库存");
        LambdaUpdateWrapper<Storage> updateWrapper = new LambdaUpdateWrapper<>();
        //等价于UPDATE t_storage SET used = used + #{count}, residue = residue - #{count} WHERE product_id = #{productId}
        //更新库存的数量，used 数量代表已经使用的库存  residue 表示剩下的库存
        updateWrapper.eq(Storage::getProductId, productId)
                .setSql("used = used + " + count + ", residue = residue - " + count);
        this.update(updateWrapper);
    }
}
